#!/bin/bash
. ./var
. ./Tmpdir
php(){
    php_version="php-7.3.9.tar.gz"
    TAR_php="php-7.3.9"
    php_prefix="/usr/local/php"

    #调用Tmpdir
    Tmpdir
    cd $dir && wget -V &> /dev/null || yum -y install wget
    [ -d /usr/local/php ] && echoRed "检测到/usr/local下已安装php，故而退出！" && rm -rf $dir && exit 1
    which php &> /dev/null
    if [ $? = 0 ];then
        echoRed "检测到已安装php，故而退出" 
        rm -rf $dir
        exit 7
    else
        echoGreen "检测到系统未安装php，即将安装"
    fi
    #安装php7.3libzip软件版本必须大于0.11，由于系统自带的版本太低，我们这里编译安装
    #参考： https://www.cnblogs.com/gyfluck/p/10478386.html
    #wget https://libzip.org/download/libzip-1.6.1.tar.gz
    wget $ip/libzip-1.2.0.tar.gz && tar xf libzip-1.2.0.tar.gz && cd libzip-1.2.0 && ./configure --prefix=/usr/local && make && make install 
    #echo $?
    if [ $? = 0 ];then
        echoGreen "libzip依赖包安装成功，现在复制zipconf.h"
        echo -e "#添加如下几行\n/usr/local/lib64\n/usr/local/lib\n/usr/lib\n/usr/lib64" >> /etc/ld.so.conf
        cat /etc/security/limits.conf
        # 使之生效
        ldconfig -v && ldconfig -v || ldconfig -v
    else
        rm -rf $dir
        exit 33
    fi

    #在安装完新版的 libzip 时可能会出现打不到 zipconf.h，手动复制一下，http://www.kuitao8.com/20180429/4689.shtml
    cd ..
    #[ ! -f /usr/local/include/zipconf.h ] 
    cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h
    #防止没有加载库文件 
    ldconfig -v && ldconfig -v || ldconfig -v
    echoGreen "libzip依赖包安装成功,现在准备安装PHP"
    cd $dir && wget $ip/${php_version} && yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel pcre-devel curl-devel
   # libzip libzip-devel 
    yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel
    tar -xf ${php_version} && cd ${TAR_php} && ./configure --prefix=${php_prefix} --with-config-file-path=/usr/local/php/etc/ --with-config-file-scan-dir=/usr/local/php/etc/ --with-libzip=/usr/local --with-jpeg-dir --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli --with-openssl --with-pcre-regex --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip && make && make install
    #make && make test && make install  我们这里不同make test
    if [ $? = 0 ];then
        echoGreen `pwd` && \cp php.ini-development ${php_prefix}/etc/php.ini && \cp ${php_prefix}/etc/php-fpm.conf.default ${php_prefix}/etc/php-fpm.conf && \cp ${php_prefix}/etc/php-fpm.d/www.conf.default ${php_prefix}/etc/php-fpm.d/www.conf && \cp -R ./sapi/fpm/init.d.php-fpm.in  /etc/init.d/php-fpm && chmod +x /etc/init.d/php-fpm
        #修改启动文件
        sed -i "s|^prefix=.*|prefix=${php_prefix}|" /etc/init.d/php-fpm
        sed -i 's|^exec_prefix=.*|exec_prefix=${prefix}|' /etc/init.d/php-fpm
        sed -i 's|^php_fpm_BIN=.*|php_fpm_BIN=${exec_prefix}/sbin/php-fpm|' /etc/init.d/php-fpm
        sed -i 's|^php_fpm_CONF=.*|php_fpm_CONF=${exec_prefix}/etc/php-fpm.conf|' /etc/init.d/php-fpm
        sed -i 's!^php_fpm_PID=.*!php_fpm_PID=${exec_prefix}/var/run/php-fpm.pid!' /etc/init.d/php-fpm
        chkconfig --add php-fpm

        #加入环境变量
        echo "export PATH=${php_prefix}/bin:${php_prefix}/sbin:$PATH" > /etc/profile.d/php7.sh
        source /etc/profile.d/php7.sh || source /etc/profile.d/php7.sh

        which php
        ${php_prefix}/bin/php -V &> /dev/null 
            if [ $? = 0 ];then
                echoGreen "已完成安装，可尽情享用！"

                #用法
                /etc/init.d/php-fpm --help
                /etc/init.d/php-fpm start && /etc/init.d/php-fpm status && echoGreen "已启动php-fpm……" || echoYellow "php-fpm启动失败……，请排查" 
                echoGreen "查看php-fpm进程信息"
                ps aux|grep php
                rm -rf $dir
            else
                echoYellow "可能配置有问题，请检查"
                exit 6
            fi
    else
        echoRed "编译出现问题了，即将退出，请查看……"
        echoYellow "解决报错：https://www.jianshu.com/p/f73cc37b1053"
        rm -rf $dir
        exit 4
    fi
}
